﻿using System;
using RBOSS.Business.CreditCard;
using RBOSS.Business.CustomerModule;
using RBOSS.Utilities;

namespace RBOSS.WebUI
{
    public partial class AddCreditCard : System.Web.UI.Page
    {
        private readonly ICustomerSubSystem _customerSubSystem = new CustomerSubSystem();
        private readonly CreditCardSubsystem _creditCardSubsystem = new CreditCardSubsystem();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["role"] != null && (string)Session["role"] == "Customer" && Session["email"] != null)
            {
                if (!IsPostBack)
                {
                    ddlCreditCard.DataSource = _creditCardSubsystem.GetAllCreditCardTypes();
                    ddlCreditCard.DataValueField = "CreditCardTypeId";
                    ddlCreditCard.DataTextField = "CreditCardTypeName";
                    ddlCreditCard.DataBind();
                }
            }
            else
            {
                Response.Redirect("Login.aspx");
            }
        }

        protected void BtnAddCreditCardClick(object sender, EventArgs e)
        {
            var customerInfo = _customerSubSystem.GetCustomerInformation((string)Session["email"]);

            if (_creditCardSubsystem.GetValidCustomerCreditCards(customerInfo.CustomerId).Count <
                _creditCardSubsystem.GetCreditCardUpperBound())
            {
                var creditCard = new CreditCard
                {
                    Address1 = txtAddressLine1.Text,
                    Address2 = txtAddressLine2.Text,
                    CreditCardNumber = RBOSSecurity.Encrypt(txtCreditCardNumber.Text),
                    CreditTypeId = Convert.ToInt32(ddlCreditCard.SelectedValue),
                    ExpiredDate = RBOSSecurity.Encrypt(txtExpirationDate.Text),
                    IsRemoved = false,
                    NameOnCard = txtNameOnCard.Text,
                    SecurityNumber = RBOSSecurity.Encrypt(txtPin.Text),
                    State = txtPin.Text,
                    Zip = txtZip.Text,
                    Country = txtCountry.Text,
                    UserId = customerInfo.CustomerId
                };

                _creditCardSubsystem.AddCreditCardInformation(creditCard);
                Message.Visible = true;
                MessageText.Text = "Credit Card AddedSuccessfully";
                hlReturnToCustomerProfilePage.Visible = true;
                ClearControls();
            }
            else
            {
                Message.Visible = true;
                MessageText.Text = "Credit Card Limit Reached...";
            }
        }

        private void ClearControls()
        {
            txtCreditCardNumber.Text = String.Empty;
            txtAddressLine1.Text = string.Empty;
            txtAddressLine2.Text = String.Empty;
            txtCity.Text = string.Empty;
            txtCountry.Text = String.Empty;
            txtCreditCardNumber.Text = string.Empty;
            txtExpirationDate.Text = String.Empty;
            txtNameOnCard.Text = string.Empty;

            txtPin.Text = string.Empty;
            txtState.Text = String.Empty;
            txtZip.Text = string.Empty;
            ddlCreditCard.ClearSelection();

        }
    }
}